package top.fzqblog;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import top.fzqblog.model.ShareInfo;

import javax.print.Doc;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

/**
 * Created by 抽离 on 2017/7/5 0005.
 */
public class PanCrawler {
    
    private String url = "http://pan.baidu.com/wap/share/home?third=0&uk=408111442";

    private static String prefix = "http://pan.baidu.com";

    @Test
    public void testCrawlerPan(){
        try {
            Document document = Jsoup
                    .connect(url)
                    .get();
            System.out.println("：" + document);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCrawlerWithHtmlUnit(){
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
//        webClient.getOptions().setJavaScriptEnabled(true);
        try {
            HtmlPage htmlPage = webClient.getPage(url);
            webClient.waitForBackgroundJavaScript(1000);
//           for(int i = 0; i < 3; i++){
//               try {
//                   Thread.sleep(5000);
//               } catch (InterruptedException e) {
//                   e.printStackTrace();
//               }
//           }
            
//            System.out.println("HtmlPage：" + htmlPage.asXml());
            Document document = Jsoup.parse(htmlPage.asXml());
            Elements elements = document.select(".list-item");
            for (Element element : elements){
                ShareInfo shareInfo = new ShareInfo();
                shareInfo.setCreateTime(new Date());
                shareInfo.setFileUrl(prefix + element.attr("href"));
                shareInfo.setFileName(element.select(".fn-main").text());
                shareInfo.setFileType(element.select(".fn-suffix").text());
                Optional.ofNullable(element.select(".list-content").text())
                        .ifPresent(
                                date ->{
                                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    try {
                                        shareInfo.setShareTime(format.parse(date.trim()));
                                    } catch (ParseException e) {
                                        e.printStackTrace();
                                    }
                                }
                        );
                System.out.println("shareInfo：" + shareInfo);
            }
            System.out.println("nextPage：" + document.select("a[node-type=next-page]").attr("href"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
